﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibrary;
using StudentHookups.WCF;
using System.Web.Security;

namespace StudentHookups.Account.Profile
{
    public partial class ViewProfile : System.Web.UI.Page
    {
        WCFClient client = new WCFClient();
        Guid requestedUser;
        protected void Page_Load(object sender, EventArgs e)
        {

            //If user got here without querystring the currently logged in users site is displayed. Otherwise
            //the user requested in the querystring is displayed
            if (Request.QueryString.HasKeys() == false)
                requestedUser = (Guid)Membership.GetUser().ProviderUserKey;
            else
            {
                requestedUser = (Guid)Membership.FindUsersByName(Request.QueryString[0])[Request.QueryString[0]].ProviderUserKey;

            }
            if (Request.IsAuthenticated)
            {
                Guid user = (Guid)Membership.GetUser().ProviderUserKey;
                UserComposite loggedinUser = client.GetUser(user);

                //To make sure the user completed his or her profile before moving on
                if ((loggedinUser == null))
                {
                    Response.Redirect("~/Account/Profile/EditProfile.aspx");
                }
                else
                {

                    loggedinUser = client.GetUser(requestedUser);

                    Fullname.Text = "<b>Fullname: </b>" + " " + loggedinUser.Firstname + " " + loggedinUser.Lastname;
                    Accountname.Text = "<b>Username: </b>" + " " + loggedinUser.Username;
                    Age.Text = "<b>Age: </b>" + " " + loggedinUser.Age.ToString();
                    Gender.Text = "<b>Gender: </b>" + " " + loggedinUser.Gender;
                    tbInfo.Text = loggedinUser.Information;
                    ProfileImage.ImageUrl = loggedinUser.Image;
                    lblmsgInfo.Text = "";

                    updateWall();
                    if (client.isFriends((Guid)Membership.GetUser().ProviderUserKey, requestedUser))
                    {
                        btnFriend.Text = "Remove friend";
                        lblFriends.Text = "You are friends";

                    }
                }
            }
        }

        protected void updateWall()
        {

            List<WallComposite> posts = client.getWallPosts(requestedUser);
            List<DisplayPosts> displayPosts = new List<DisplayPosts>();

            foreach (WallComposite post in posts)
            {
                DisplayPosts dpost = new DisplayPosts(
                    post.Title,
                    post.Message,
                    Membership.GetUser(post.Sender).UserName,
                    post.SentDate);
                displayPosts.Add(dpost);
                    
            }
            displayPosts = displayPosts.OrderByDescending(o => o.sentdate).ToList();
            gridWall.DataSource = displayPosts;
            gridWall.DataBind();
           
        }

        protected void btnFriend_Click(object sender, EventArgs e)
        {
            if (btnFriend.Text == "Remove friend")
            {
                Fullname.Text = "hej";
                bool success = client.deleteFriend((Guid)Membership.GetUser().ProviderUserKey, requestedUser);
                if (success)
                {
                    lblFriends.Text = "You are not friends";
                    btnFriend.Text = "Add as friend";
                }
            }
                
            else{
                if (!(requestedUser == (Guid)Membership.GetUser().ProviderUserKey))
                {
                    bool success = client.becomeFriends((Guid)Membership.GetUser().ProviderUserKey, requestedUser);

                    if (client.isFriends((Guid)Membership.GetUser().ProviderUserKey, requestedUser))
                    {
                        lblFriends.Text = "You are friends";
                        btnFriend.Text = "Remove friend";
                        

                    }
                }
                else
                {
                    lblFriends.Text = "Cant add self as friend";
                }
            }
           
        }

        protected void btnSendMsg_Click(object sender, EventArgs e)
        {
            MessageComposite msg = new MessageComposite();
            msg.MessageID = Guid.NewGuid();
            msg.Message = tbSendMessage.Text;
            msg.Sender = (Guid)Membership.GetUser().ProviderUserKey;
            msg.Recepient = requestedUser;
            msg.Sentdate = DateTime.Now.ToString();
            msg.Viewed = false;

            bool success = client.sendMessage(msg);
            if (success)
            {
                tbSendMessage.Text = "";
                lblmsgInfo.Text = "Message Sent!";
            }
            else
            {
                lblmsgInfo.Text = "Message did not get sent please try again later!";
            }
        }

        protected void btnCancelMsg_Click(object sender, EventArgs e)
        {
            tbSendMessage.Text = "";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            WallComposite post = new WallComposite();
            post.MessageID = Guid.NewGuid();
            post.Title = tbWallTitle.Text;
            post.Message = tbWallMessage.Text;
            post.SentDate = DateTime.Now.ToString();
            post.Sender = (Guid)Membership.GetUser().ProviderUserKey;
            post.Recepient = requestedUser;

            bool success = client.postOnWall(post);
            if (success)
            {
                tbWallTitle.Text = "HEJHEJ";
                updateWall();
            }
        }

        protected void btnCancelPost_Click(object sender, EventArgs e)
        {
            
        }
    }
}